﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;

using HsTec.Mobile.Green.Util;

namespace HsTec.Mobile.Green.DAL
{
    /// <summary>
    /// 数据访问类GoalOriente。
    /// </summary>
    public class GoalOrienteDAL
    {
        public GoalOrienteDAL() { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string sExistsWhere)
        {
            return SQLHelper.Exists("tb_GoalOriente", sExistsWhere);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(HsTec.Mobile.Green.Model.GoalOriente model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tb_GoalOriente(");
            strSql.Append("Years,TrafficForecast,SetFrequency,ConsumptionAccounting,TelecommunicationTraffic,EnergyAccounting,Type,AddDate,OperatUser,OperatDate,Remark)");
            strSql.Append(" values (");
            strSql.Append("@Years,@TrafficForecast,@SetFrequency,@ConsumptionAccounting,@TelecommunicationTraffic,@EnergyAccounting,@Type,@AddDate,@OperatUser,@OperatDate,@Remark)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@Years", SqlDbType.Char,4),
					new SqlParameter("@TrafficForecast", SqlDbType.Decimal,13),
					new SqlParameter("@SetFrequency", SqlDbType.Decimal,13),
					new SqlParameter("@ConsumptionAccounting", SqlDbType.Decimal,13),
					new SqlParameter("@TelecommunicationTraffic", SqlDbType.Decimal,13),
					new SqlParameter("@EnergyAccounting", SqlDbType.Decimal,13),
					new SqlParameter("@Type", SqlDbType.Char,1),
					new SqlParameter("@AddDate", SqlDbType.DateTime),
					new SqlParameter("@OperatUser", SqlDbType.Int,4),
					new SqlParameter("@OperatDate", SqlDbType.DateTime),
					new SqlParameter("@Remark", SqlDbType.VarChar,200)};
            parameters[0].Value = model.Years;
            parameters[1].Value = model.TrafficForecast;
            parameters[2].Value = model.SetFrequency;
            parameters[3].Value = model.ConsumptionAccounting;
            parameters[4].Value = model.TelecommunicationTraffic;
            parameters[5].Value = model.EnergyAccounting;
            parameters[6].Value = model.Type;
            parameters[7].Value = model.AddDate;
            parameters[8].Value = model.OperatUser;
            parameters[9].Value = model.OperatDate;
            parameters[10].Value = model.Remark;

            return SQLHelper.ExecuteNonQuery(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update(HsTec.Mobile.Green.Model.GoalOriente model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tb_GoalOriente set ");
            strSql.Append("Years=@Years,");
            strSql.Append("TrafficForecast=@TrafficForecast,");
            strSql.Append("SetFrequency=@SetFrequency,");
            strSql.Append("ConsumptionAccounting=@ConsumptionAccounting,");
            strSql.Append("TelecommunicationTraffic=@TelecommunicationTraffic,");
            strSql.Append("EnergyAccounting=@EnergyAccounting,");
            strSql.Append("Type=@Type,");
            strSql.Append("AddDate=@AddDate,");
            strSql.Append("OperatUser=@OperatUser,");
            strSql.Append("OperatDate=@OperatDate,");
            strSql.Append("Remark=@Remark");
            strSql.Append(" where GO_ID=@GO_ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@GO_ID", SqlDbType.Int,4),
					new SqlParameter("@Years", SqlDbType.Char,4),
					new SqlParameter("@TrafficForecast", SqlDbType.Decimal,13),
					new SqlParameter("@SetFrequency", SqlDbType.Decimal,13),
					new SqlParameter("@ConsumptionAccounting", SqlDbType.Decimal,13),
					new SqlParameter("@TelecommunicationTraffic", SqlDbType.Decimal,13),
					new SqlParameter("@EnergyAccounting", SqlDbType.Decimal,13),
					new SqlParameter("@Type", SqlDbType.Char,1),
					new SqlParameter("@AddDate", SqlDbType.DateTime),
					new SqlParameter("@OperatUser", SqlDbType.Int,4),
					new SqlParameter("@OperatDate", SqlDbType.DateTime),
					new SqlParameter("@Remark", SqlDbType.VarChar,200)};
            parameters[0].Value = model.GO_ID;
            parameters[1].Value = model.Years;
            parameters[2].Value = model.TrafficForecast;
            parameters[3].Value = model.SetFrequency;
            parameters[4].Value = model.ConsumptionAccounting;
            parameters[5].Value = model.TelecommunicationTraffic;
            parameters[6].Value = model.EnergyAccounting;
            parameters[7].Value = model.Type;
            parameters[8].Value = model.AddDate;
            parameters[9].Value = model.OperatUser;
            parameters[10].Value = model.OperatDate;
            parameters[11].Value = model.Remark;

            return SQLHelper.ExecuteNonQuery(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public int Delete(string sIDs)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tb_GoalOriente ");
            strSql.Append(" where GO_ID in ( " + sIDs + " )");
            return SQLHelper.ExecuteNonQuery(strSql.ToString());
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public HsTec.Mobile.Green.Model.GoalOriente GetModel(int GO_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 GO_ID,Years,TrafficForecast,SetFrequency,ConsumptionAccounting,TelecommunicationTraffic,EnergyAccounting,Type,AddDate,OperatUser,OperatDate,Remark from tb_GoalOriente ");
            strSql.Append(" where GO_ID=@GO_ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@GO_ID", SqlDbType.Int,4)};
            parameters[0].Value = GO_ID;

            HsTec.Mobile.Green.Model.GoalOriente model = new HsTec.Mobile.Green.Model.GoalOriente();
            DataSet ds = SQLHelper.ExecuteDataset(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["GO_ID"].ToString() != "")
                {
                    model.GO_ID = int.Parse(ds.Tables[0].Rows[0]["GO_ID"].ToString());
                }
                model.Years = ds.Tables[0].Rows[0]["Years"].ToString();
                if (ds.Tables[0].Rows[0]["TrafficForecast"].ToString() != "")
                {
                    model.TrafficForecast = decimal.Parse(ds.Tables[0].Rows[0]["TrafficForecast"].ToString());
                }
                if (ds.Tables[0].Rows[0]["SetFrequency"].ToString() != "")
                {
                    model.SetFrequency = decimal.Parse(ds.Tables[0].Rows[0]["SetFrequency"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ConsumptionAccounting"].ToString() != "")
                {
                    model.ConsumptionAccounting = decimal.Parse(ds.Tables[0].Rows[0]["ConsumptionAccounting"].ToString());
                }
                if (ds.Tables[0].Rows[0]["TelecommunicationTraffic"].ToString() != "")
                {
                    model.TelecommunicationTraffic = decimal.Parse(ds.Tables[0].Rows[0]["TelecommunicationTraffic"].ToString());
                }
                if (ds.Tables[0].Rows[0]["EnergyAccounting"].ToString() != "")
                {
                    model.EnergyAccounting = decimal.Parse(ds.Tables[0].Rows[0]["EnergyAccounting"].ToString());
                }
                model.Type = ds.Tables[0].Rows[0]["Type"].ToString();
                if (ds.Tables[0].Rows[0]["AddDate"].ToString() != "")
                {
                    model.AddDate = DateTime.Parse(ds.Tables[0].Rows[0]["AddDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["OperatUser"].ToString() != "")
                {
                    model.OperatUser = int.Parse(ds.Tables[0].Rows[0]["OperatUser"].ToString());
                }
                if (ds.Tables[0].Rows[0]["OperatDate"].ToString() != "")
                {
                    model.OperatDate = DateTime.Parse(ds.Tables[0].Rows[0]["OperatDate"].ToString());
                }
                model.Remark = ds.Tables[0].Rows[0]["Remark"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataView GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select GO_ID,Years,TrafficForecast,SetFrequency,ConsumptionAccounting,TelecommunicationTraffic,EnergyAccounting,Type,AddDate,OperatUser,OperatDate,Remark ");
            strSql.Append(" FROM tb_GoalOriente ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where (1=1) " + strWhere);
            }
            strSql.Append(" order by  Years desc ");
            return SQLHelper.ExecuteDataView(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataView GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" GO_ID,Years,TrafficForecast,SetFrequency,ConsumptionAccounting,TelecommunicationTraffic,EnergyAccounting,Type,AddDate,OperatUser,OperatDate,Remark ");
            strSql.Append(" FROM tb_GoalOriente ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return SQLHelper.ExecuteDataView(strSql.ToString());
        }

        #endregion  成员方法
    }
}

